/*
 * JBeagle - a Java toolkit for genetic algorithms.
 * 
 * Copyright (c) 2010 Matthijs Snel
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package jbeagle.core;

import java.util.Collection;

public class IntegerIndividualFactory extends ListIndividualFactory<Integer> {

	public static final int	DEFAULT_MIN_INIT = 0,
							DEFAULT_MAX_INIT = 1;
	
	protected int mininit, maxinit;
	
	public IntegerIndividualFactory( int minlen, int maxlen ) {
		this( minlen, maxlen, DEFAULT_MIN_INIT, DEFAULT_MAX_INIT );
	}
	
	public IntegerIndividualFactory( int minlen, int maxlen, int mininit, int maxinit ) {
		super( minlen, maxlen );
		this.mininit = mininit;
		this.maxinit = maxinit;
	}
   
    public void setIndividualBoundaries(final int minval, final int maxval) {
        setConstraints( new Constraints<Integer>(){
            protected boolean violates(Integer i) {
                if(i < minval || i > maxval)
                    return true;
                return false;
            }
        });
    }

    @Override
	protected void fillIndividual( ListIndividual<Integer> ind, int len) {
		for ( int i = 0; i < len; i++ )
			ind.add( (int) Math.round(mininit + Math.random() * (maxinit - mininit)) );
	}
    
    @Override
	protected void fillIndividual( ListIndividual<Integer> ind, Collection<String> values) {
		for ( String val : values )
			ind.add( Integer.parseInt(val) );
	}
}

